<?php


namespace app\cms\dto;


use iflow\annotation\value\validate\ValidateRule;
use iflow\Container\implement\annotation\tools\data\NotNull;

class bindPhoneOrEmail
{

    protected array $userInfo = [];

    private string $emailRegx = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})";
    private string $phoneRegx = "/^1[3|4|5|7|8|9]{1}\d{9}$/";

    public string $oldAccount = "";
    #[NotNull(error: '手机号或者邮箱不能为空')]
    public string $account;

    #[ValidateRule([
        'code' => 'required',
        'uuid' => 'required',
    ], [
        'code.required' => '验证码不能为空',
        'uuid.required' => '验证识别码不能为空'
    ])]
    public array $sms = [
        'code' => '',
        'uuid' => ''
    ]; // 注册验证码

    public function getUid()
    {
        return session('userInfo')['uid'];
    }

    public function getChangeAccountType(): string|bool
    {
        if (preg_match($this -> emailRegx, $this->account)) return 'email';
        if (preg_match($this -> phoneRegx, $this->account)) return 'phone';
        return false;
    }

    public function checkOldAccount(string $account, string $type): bool
    {
        return preg_match($type === 'email' ? $this->emailRegx : $this->phoneRegx, $account);
    }
}